﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using TouchRC.Classes;
using System.Drawing;
using TouchRC.Logic;
using Microsoft.Drawing;

namespace TouchRC.Custom_Controls
{
    public class MenuOptionsPanel : FluidPanel
    {
        private List<MenuButton> m_Options = new List<MenuButton>();

        public MenuOptionsPanel()
        {
        }

        public MenuOptionsPanel(List<MainMenuOption> options)
        {
            foreach (MainMenuOption option in options)
            {
                MenuButton button = new MenuButton(option.Option);
                button.BackColor = Color.Empty;
                button.Image = option.Image;
                button.Visible = true;

                m_Options.Add(button);
                Controls.Add(button);
            }
        }

        protected override void InitControl()
        {
            BackColor = Color.Blue;
            base.InitControl();
        }

        public void SetPositions()
        {
            int width = UnscaleX(Width);
            int height = UnscaleY(Height);

            int heightMargin = 0;

            bool moreThanTwo = m_Options.Count > 2;

            if (AppSettings.CurrentOrientation == ScreenOrientation.Landscape)
            {
                heightMargin = UnscaleY(Bounds.Y) / 2;
            }

            int imageSize = AppSettings.ButtonDimensions * 2;
            int margin = 10;

            int middleXPos = (width - imageSize) / 2;
            int leftXPos = margin;
            int rightXPos = width - (imageSize + margin);

            int topYPos = margin - heightMargin;
            int bottomYPos = height - (imageSize + margin) - heightMargin;
            int middleYPos = ((height - imageSize) / 2) - heightMargin;

            int counter = 0;

            foreach (MenuButton button in m_Options)
            {
                int xPos = 0;
                int yPos = 0;

                if ((counter + 1) == m_Options.Count && counter % 2 == 0)
                    xPos = middleXPos;
                else if (counter % 2 == 0)
                    xPos = leftXPos;
                else
                    xPos = rightXPos;

                if (moreThanTwo)
                {
                    if (counter < 2)
                        yPos = topYPos;
                    else
                        yPos = bottomYPos;
                }
                else
                {
                    yPos = middleYPos;
                }

                xPos = ScaleX(xPos);
                yPos = ScaleY(yPos);

                imageSize = ScaleX(imageSize);

                button.Bounds = new Rectangle(xPos, yPos, imageSize, imageSize);

                counter++;
            }
        }
    }
}
